package com.nl.countdownclock.client;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.dom.client.CanvasElement;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.user.client.Timer;
import com.nl.countdownclock.client.MatrixDigit;
import com.nl.countdownclock.client.ColonSpacer;

import com.nl.countdownclock.client.PhysicalEnsemble;
import com.nl.countdownclock.client.PhysicalEnsembleView;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.RootPanel;

@SuppressWarnings("deprecation")
public class ClockView {
	final static String DAYCOLOR = "265897";
	final static String HOURSCOLOR = "13acfa";
	final static String MINUTESCOLOR = "c0000b";
	final static String SECONDSCOLOR = "009a49";
	final static String EMPTYCOLOR = "c9c9c9";
	final static String COLONCOLOR = "b6b4b5";
	String[] ballColor = { DAYCOLOR,DAYCOLOR,DAYCOLOR,	HOURSCOLOR, HOURSCOLOR, MINUTESCOLOR, MINUTESCOLOR, SECONDSCOLOR, SECONDSCOLOR };

	Clock clock;
	Canvas canvas;
	Context2d context = null;
	PhysicalEnsembleView peView;
	PhysicalEnsemble pe;
	double clockWidth;
	double clockHeight;
	double ballDiameter;
	double ballSpacing;
	double clockULx;
	double clockULy;
	final int digitCount = 9;
	MatrixDigit[] digits = new MatrixDigit[digitCount];
	ColonSpacer[] spacers = new ColonSpacer[3];
	public final double horizFraction = 0.85;
	public final int frameTime = 30;
	public ClockView(Canvas canvas, Clock clock){
		this.canvas = canvas;
		this.clock = clock;
		init();		
	}
	private void init(){
        context = canvas.getContext2d();
        pe = new PhysicalEnsemble();
        peView = new PhysicalEnsembleView( canvas, pe );
        final Timer timer = new Timer() {
            @Override
            public void run() {
                ClockView.this.drawLoop();
            }
        };
        doLayout();
        timer.scheduleRepeating(frameTime);	
        Window.addWindowResizeListener(new WindowResizeListener(){

			@Override
			public void onWindowResized(int width, int height) {
				// TODO Auto-generated method stub
				doLayout();
			}
        	
        });
        
	}
	public void doLayout(){
		final int colonCount = 3;
		final int intradigitSpacing = 1;
		final int intradigitCount = 5;
		final int colonSize = 3;
		final int digitWidth = 4;
		final int digitHeight = 7;
		final int ballColumns = digitCount * digitWidth + intradigitCount * intradigitSpacing + colonSize * colonCount;
		final double ballFraction = 0.75;  // 
		final double marginFraction = 0.20; // fraction to reserve for margin
		final int maxWidth = 1200; // maximum width of clock;
		
		
		// handle the resizing of the canvas here --- ugh, must fix
		RootPanel rootPanel = RootPanel.get("clockbody");
        int canvasHeight = rootPanel.getOffsetHeight();
        int canvasWidth = rootPanel.getOffsetWidth();
        canvas.setWidth(canvasWidth + "px");
//        canvas.setHeight("100%");
        canvas.setStyleName("maincanvas");
    	canvas.setCoordinateSpaceWidth(canvasWidth);
    	canvas.setCoordinateSpaceHeight(canvasHeight);
 
		
		
		
		
		CanvasElement ce = canvas.getCanvasElement();
    	int width = Math.min(maxWidth, ce.getClientWidth());
    	clockWidth = width * (1.0 - marginFraction );
    	clockHeight = clockWidth * digitHeight / ballColumns;
    	ballSpacing = clockWidth / ballColumns;
    	ballDiameter = ballSpacing * ballFraction ;
    	// assume clock is centered vertically and horizontally for now
    	clockULx = ( ce.getClientWidth() - clockWidth )/2.0;
    	clockULy = ( ce.getClientHeight() - clockHeight )/2.0;
    	// create the digits
    	double tempdigitspacex = ballSpacing * ( digitWidth + intradigitSpacing );
    	double y = clockULy;
    	double x = clockULx;
    	int j = 0;
    	for(int i = 0; i < digitCount; i++){
    		digits[i] = new MatrixDigit(context, x, y,  ballSpacing, ballSpacing, ballDiameter/2.0, ballColor[i], EMPTYCOLOR);
    		x += tempdigitspacex;
    		if ( ( i == 2) || (i == 4) || ( i == 6) ){
    			spacers[j++] = new ColonSpacer(context, x, y,  ballSpacing, ballSpacing, ballDiameter/2.0, COLONCOLOR);
    			x += 2 * ballSpacing;
    		}
    		}
    	}
		
	
	private void drawDigits(){
		int[] toDraw = clock.getDigits();
		for(int i = 0; i < digitCount; i++)
			digits[i].drawDigit(toDraw[i]);
		for (int j = 0; j < 3; j++)
			spacers[j].drawColon();
		
	}

	private void drawLoop(){
//		doLayout();   // this belongs in size changed handler
		CanvasElement ce = canvas.getCanvasElement();
    	int width = ce.getClientWidth();
    	ce.setWidth(width); // This will clear the canvas;
    	drawDigits();

    	pe.evolve();
    	peView.draw();
	}
}
